package com.glxy.controller;

import com.glxy.pojo.Provider;
import com.glxy.pojo.User;
import com.glxy.service.impl.ProviderServicelmpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

public class ProviderServlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session = req.getSession();
        String action = req.getParameter("action");
        ProviderServicelmpl providerService = new ProviderServicelmpl();

        if(action.equals("list")){
            int pageNo =1;
            String pageNos = req.getParameter("pageNo");
            if(pageNos != null){
                pageNo = Integer.parseInt(pageNos);
            }
            int pageSize = 5;
            int totalCount = providerService.totalCount();
            int totalPage = 0;
            if(totalCount%pageSize == 0){
                totalPage = totalCount/pageSize;
            }
            else{
                totalPage = totalCount/pageSize+1;
            }
            int prev = pageNo;
            int next = pageNo;
            if(pageNo > 1){
                prev --;
            }
            if(pageNo < totalPage){
                next++;
            }
            List<Provider> list = providerService.findAllProviderPages(pageSize,pageNo);
            req.setAttribute("totalPage",totalPage);
            req.setAttribute("prev",prev);
            req.setAttribute("next",next);
            req.setAttribute("pageNo",pageNo);
            req.setAttribute("list",list);
            req.getRequestDispatcher("providerList.jsp").forward(req,resp);
        } else if (action.equals("search")) {
            String proName =  req.getParameter("proName");
            ArrayList<Provider> list = providerService.findProvider(proName);
            req.setAttribute("list",list);
            req.getRequestDispatcher("providerList.jsp").forward(req,resp);
        }else if (action.equals("add")) {

            Provider provider =  new Provider();
            provider.setId(Long.valueOf(req.getParameter("providerId")));
            provider.setProCode(req.getParameter("providerCode"));
            provider.setProName(req.getParameter("providerName"));
            provider.setProContact(req.getParameter("people"));
            provider.setProPhone(req.getParameter("phone"));
            provider.setAddress(req.getParameter("address"));
            provider.setProFax(req.getParameter("fax"));
            provider.setProDesc(req.getParameter("describe"));
            providerService.insertProvider(provider);
            req.getRequestDispatcher("ProviderServlet?action=list").forward(req,resp);

        } else if (action.equals("view")) {
            Provider provider = providerService.findProvider(req.getParameter("name")).get(0);

            req.setAttribute("provider",provider);

            req.getRequestDispatcher("providerView.jsp").forward(req,resp);

        } else if(action.equals("update")){
            String name = req.getParameter("name");

            Provider provider = providerService.findProvider(name).get(0);

            req.setAttribute("provider", provider);
            req.getRequestDispatcher("providerUpdate.jsp").forward(req, resp);

        }else if (action.equals("updateSave")){
            Provider provider = providerService.findProvider(req.getParameter("oriName")).get(0);

            provider.setId(Long.valueOf(req.getParameter("providerId")));
            provider.setProName(req.getParameter("providerName"));
            provider.setProContact(req.getParameter("people"));
            provider.setProPhone(req.getParameter("phone"));
            provider.setAddress(req.getParameter("address"));
            provider.setProFax(req.getParameter("fax"));
            provider.setProDesc(req.getParameter("describe"));

            providerService.update(provider);

            req.getRequestDispatcher("UserServlet?action=list").forward(req,resp);
        }else if(action.equals("delete")){
            String name = req.getParameter("name");

            Provider provider = providerService.findProvider(name).get(0);

            providerService.delete(provider.getId());

            req.getRequestDispatcher("BillServlet?action=list").forward(req, resp);
        }else{
            System.out.println("两次密码不一致");
        }

    }

}
